package com.jron.cqiu.kernel.scheduler;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

import com.jron.cqiu.util.ConfigurationUtil;
import com.jron.cqiu.kernel.handlers.IHotKeyRegister;

/**
 * Created by Intellij IDEA.
 *
 * @Author: Yejw
 * @Date: 2008-6-21
 * @Time: 5:19:37
 */
public class KingTrigger implements IKingTrigger {

    private static Log log = LogFactory.getLog(KingTrigger.class);

    public void run() {
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = null;

        try {
            sched = sf.getScheduler();
            long mailOrderRi = Long.parseLong(ConfigurationUtil.value("order.mail.trigger.repeatinterval"));
            long killTaskRi = Long.parseLong(ConfigurationUtil.value("order.kill.task.trigger.repeatinterval"));

            //mail order
            JobDetail job = new JobDetail("MailOrderJob", kingGroup, MailOrderScheduler.class);
            SimpleTrigger trigger = new SimpleTrigger("MailOrderTrigger", kingGroup,
                    new Date(baseStartTime), null, SimpleTrigger.REPEAT_INDEFINITELY, mailOrderRi * 1000l);
            sched.scheduleJob(job, trigger);

            //startup check
            job = new JobDetail("AutorunJob", kingGroup, StartupScheduler.class);
            Trigger trig = TriggerUtils.makeHourlyTrigger("AutorunTrigger");
            trig.setStartTime(new Date(baseStartTime + 20));
            sched.scheduleJob(job, trig);

            //kill tasklist check
            job = new JobDetail("KillTaskJob", kingGroup, TaskScheduler.class);
            trigger = new SimpleTrigger("KillTaskTrigger", kingGroup,
                    new Date(baseStartTime + 40), null, SimpleTrigger.REPEAT_INDEFINITELY, killTaskRi * 1000l);
            sched.scheduleJob(job, trigger);

            sched.start();
        } catch (SchedulerException e) {
            log.warn(e.getMessage());
        }

        IHotKeyRegister.INSTANCE.registerHotkey();

    }

}
